<?php

namespace App\Admin\Extensions;

use App\Models\Statistic;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Layout\Column;
use Encore\Admin\Widgets\InfoBox;
use App\Services\Statistics\SearchCount;
use Carbon\Carbon;
use Brick\Math\RoundingMode;


class GoldStatisticsBoxes
{
	public function __construct()
	{
		$this->start = request('start') ?? Carbon::today()->toDateString();
		$this->end = request('end') ?? Carbon::tomorrow()->toDateString();

		$this->result = ['start'=> $this->start , 'end'=> $this->end];

		$this->ownProductApple = SearchCount::searchOwnAppleProduce($this->result);
		$this->oneLevelTeamAppleProduceA = SearchCount::searchOneLevelTeamAppleProduce($this->result);
		$this->twoLevelTeamAppleProduceA = SearchCount::searchTwoLevelTeamAppleProduce($this->result);
		$this->appleTreeProduceA = SearchCount::searchAppleTreeProduce($this->result);
		
		
	}

	public function countBoxes()
	{
		return function (Row $row) {
			
			$this->vipChargeMoney($row, 2);
			$this->vipChargeMoneyByLink($row, 2);
			$this->vipIncome($row, 2);
			$this->newVipChargeMoney($row, 2);
			$this->vipCount($row, 2);
			$this->vipCountDay($row, 2);


		};
	}

	public function countBoxes1()
	{
		return function (Row $row) {
			$this->ownAppleProduce($row, 2);
			$this->oneLevelTeamAppleProduce($row, 2);
			$this->twoLevelTeamAppleProduce($row, 2);
			$this->appleTreeProduce($row, 2);
		};
	}

	public function countBoxes2()
	{
		return function (Row $row) {
			$this->applesExchangeBalances($row, 2);
			$this->applesExchangeOrders($row, 2);
		};
	}

	public function countBoxes3()
	{
		return function (Row $row) {
			$this->vipCost($row, 2);
			$this->vipActualCost($row, 2);
		};
	}

	public function countBoxes9()
	{
		return function (Row $row) {
			
			$this->vipProfit($row, 2);
			$this->vipActualProfit($row, 2);
		};
	}


	public function countBoxes4()
	{
		return function (Row $row) {
			$this->commanderWages($row, 2);
			$this->commissionerWages($row, 2);
			$this->majordomoWages($row, 2);
			$this->traderWages($row, 2);
			$this->technologyWages($row, 2);
			$this->videoWages($row, 2);
		};
	}

	public function countBoxes5()
	{
		return function (Row $row) {
			$this->userBalances($row, 2);
			$this->withdrawMoney($row, 2);
			$this->withdrawChargeMoney($row, 2);
		};
	}

	public function countBoxes6()
	{
		return function (Row $row) {
			$this->wxPayOrder($row, 2);
			$this->alipayPayOrder($row, 2);
			$this->balanceOrder($row, 2);
			$this->orderIncome($row, 2);

		};
	}

	public function countBoxes7()
	{
		return function (Row $row) {
			$this->productCostTotal($row, 2);
			$this->ownRewards($row, 2);
			$this->oneLevelRewards($row, 2);
			$this->twoLevelRewards($row, 2);
			$this->orderCost($row, 2);
			
			
		};
	}

	public function countBoxes8()
	{
		return function (Row $row) {
			$this->preMonthRewards($row, 2);
			$this->monthRewards($row, 2);
			$this->orderProfit($row, 2);
			
		};
	}


	protected function vipChargeMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchVipChargesMoney($this->result);
			$box = new InfoBox('会员卡线上收入', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}

	protected function vipChargeMoneyByLink(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchVipChargesMoneyByLink($this->result);
			$box = new InfoBox('会员卡线下收入', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}

	protected function newVipChargeMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchNewVipChargeMoney($this->result);
			$box = new InfoBox('新增会员卡收入统计', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}


	protected function ownAppleProduce(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = $this->ownProductApple;
			$box = new InfoBox('我的苹果产出', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}

	protected function oneLevelTeamAppleProduce(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = $this->oneLevelTeamAppleProduceA;
			$box = new InfoBox('一区苹果产出', 'bitcoin', 'purple', '/admin/hipster-apples', $money);
			$column->append($box);
		});
	}

	protected function twoLevelTeamAppleProduce(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = $this->twoLevelTeamAppleProduceA;
			$box = new InfoBox('二区苹果产出', 'bitcoin', 'purple', '/admin/hipster-apples', $money);
			$column->append($box);
		});
	}
	
	protected function appleTreeProduce(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = $this->appleTreeProduceA;
			$box = new InfoBox('变异苹果产出', 'bitcoin', 'purple', '/admin/hipster-apples', $money);
			$column->append($box);
		});
	}

	protected function applesExchangeBalances(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchApplesExchangeBalances($this->result);
			$box = new InfoBox('苹果兑换余额', 'bitcoin', 'purple', '/admin/hipster-apples', abs($money));
			$column->append($box);
		});
	}

	protected function applesExchangeOrders(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchApplesExchangeOrders($this->result);
			$box = new InfoBox('苹果兑换商品', 'bitcoin', 'purple', '/admin/hipster-apples', abs($money));
			$column->append($box);
		});
	}
	

	protected function withdrawMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchWithdrawActualMoney($this->result);
			$box = new InfoBox('提现总额', 'bitcoin', 'purple', '/admin/withdraws', $money);
			$column->append($box);
		});
	}

	protected function withdrawChargeMoney(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = abs(SearchCount::searchWithdrawChargeMoney($this->result));
			$box = new InfoBox('提现手续费总额', 'bitcoin', 'purple', '/admin/withdraws', $money);
			$column->append($box);
		});
	}
	

	protected function userBalances(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchUserBalances();
			$box = new InfoBox('用户余额', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function commanderWages(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchCommanderWages($this->result);
			$box = new InfoBox('团长工资', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function commissionerWages(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchCommissionerrWages($this->result);
			$box = new InfoBox('运营专员工资', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function majordomoWages(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchMajordomoWages($this->result);
			$box = new InfoBox('总监工资', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function traderWages(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchTraderWages($this->result);
			$box = new InfoBox('操盘手工资', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function technologyWages(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchTechnologyWages($this->result);
			$box = new InfoBox('技术工资', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function videoWages(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchVideoWages($this->result);
			$box = new InfoBox('视频团队工资', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function wxPayOrder(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchWxPayOrder($this->result);
			$box = new InfoBox('微信支付订单', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function alipayPayOrder(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchAliPayOrder($this->result);
			$box = new InfoBox('支付宝支付订单', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}


	protected function balanceOrder(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchBalanceOrder($this->result);
			$box = new InfoBox('余额支付订单', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function productCostTotal(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchProductCost($this->result);
			$box = new InfoBox('全部发货成本统计', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	

	protected function ownRewards(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchOwnRewards($this->result);
			$box = new InfoBox('自购佣金', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function oneLevelRewards(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchOneLevelRewards($this->result);
			$box = new InfoBox('发放直推佣金', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function twoLevelRewards(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchTwoLevelRewards($this->result);
			$box = new InfoBox('发放团队佣金', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function preMonthRewards(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchPreMonthRewards();
			$box = new InfoBox('上月预估发放', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	protected function monthRewards(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = SearchCount::searchMonthRewards();
			$box = new InfoBox('本月预估发放', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

/*

	protected function productCost(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =SearchCount::searchProductCostIsDelivered($this->result);
			$box = new InfoBox('已发货成本统计', 'bitcoin', 'purple', '/admin/user', $money);
			$column->append($box);
		});
	}

	
*/
	protected function vipCount(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchVipCount($this->result));
			$box = new InfoBox('开通会员总数', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}

	protected function vipCountDay(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchVipCountDay($this->result));
			$box = new InfoBox('查询时间内开通会员数', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}


	protected function vipIncome(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchVipChargesMoney($this->result))
					->plus(SearchCount::searchVipChargesMoneyByLink($this->result));
			$box = new InfoBox('会员收入统计', 'bitcoin', 'purple', '/admin/vip-purchases', $money);
			$column->append($box);
		});
	}

	protected function orderIncome(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchWxPayOrder($this->result))
					->plus(SearchCount::searchAliPayOrder($this->result))
					->plus(SearchCount::searchBalanceOrder($this->result));
			$box = new InfoBox('卖货收入统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function orderCost(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchProductCost($this->result))
					->plus(SearchCount::searchOwnRewards($this->result))
					->plus(SearchCount::searchOneLevelRewards($this->result))
					->plus(SearchCount::searchTwoLevelRewards($this->result));
			$box = new InfoBox('卖货成本统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function orderProfit(Row $row, $column)
	{
		$row->column($column, function (Column $column){

			$money = big_decimal(SearchCount::searchWxPayOrder($this->result))
					->plus(SearchCount::searchAliPayOrder($this->result))
					->plus(SearchCount::searchBalanceOrder($this->result))
					->minus(big_decimal(SearchCount::searchProductCost($this->result)))
					->minus(SearchCount::searchOwnRewards($this->result))
					->minus(SearchCount::searchOneLevelRewards($this->result))
					->minus(SearchCount::searchTwoLevelRewards($this->result));
			$box = new InfoBox('卖货利润统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}


	protected function vipCost(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(big_decimal($this->ownProductApple)->multipliedBy(cs('sale')->apple_actual_price))
					->plus(big_decimal($this->oneLevelTeamAppleProduceA)->multipliedBy(cs('sale')->apple_actual_price))
					->plus(big_decimal($this->twoLevelTeamAppleProduceA)->multipliedBy(cs('sale')->apple_actual_price))
					->plus(big_decimal($this->appleTreeProduceA )->multipliedBy(cs('sale')->apple_actual_price))
					->plus(SearchCount::searchCommanderWages($this->result))
					->plus(SearchCount::searchCommissionerrWages($this->result))
					->plus(SearchCount::searchMajordomoWages($this->result))
					->plus(SearchCount::searchTraderWages($this->result))
					->plus(SearchCount::searchTechnologyWages($this->result))
					->plus(SearchCount::searchVideoWages($this->result))
					->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('预估会员成本统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function vipActualCost(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money = big_decimal(SearchCount::searchCommanderWages($this->result))
					->plus(SearchCount::searchCommissionerrWages($this->result))
					->plus(SearchCount::searchMajordomoWages($this->result))
					->plus(SearchCount::searchTraderWages($this->result))
					->plus(SearchCount::searchTechnologyWages($this->result))
					->plus(SearchCount::searchVideoWages($this->result))
					->plus(abs(SearchCount::searchApplesExchangeBalances($this->result)))
					->plus(abs(SearchCount::searchApplesExchangeOrders($this->result)))
					->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('实际会员成本统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function vipActualProfit(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =   big_decimal(SearchCount::searchVipChargesMoney($this->result))
					->plus(SearchCount::searchVipChargesMoneyByLink($this->result))
					->minus(SearchCount::searchCommanderWages($this->result))
					->minus(SearchCount::searchCommissionerrWages($this->result))
					->minus(SearchCount::searchMajordomoWages($this->result))
					->minus(SearchCount::searchTraderWages($this->result))
					->minus(SearchCount::searchTechnologyWages($this->result))
					->minus(SearchCount::searchVideoWages($this->result))
					->minus(abs(SearchCount::searchApplesExchangeBalances($this->result)))
					->minus(abs(SearchCount::searchApplesExchangeOrders($this->result)))
					->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('实际会员利润统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}

	protected function vipProfit(Row $row, $column)
	{
		$row->column($column, function (Column $column){
			$money =   big_decimal(SearchCount::searchVipChargesMoney($this->result))
					->plus(SearchCount::searchVipChargesMoneyByLink($this->result))
					->minus(big_decimal(big_decimal($this->ownProductApple)->multipliedBy(cs('sale')->apple_actual_price)))
					->minus(big_decimal($this->oneLevelTeamAppleProduceA)->multipliedBy(cs('sale')->apple_actual_price))
					->minus(big_decimal($this->twoLevelTeamAppleProduceA )->multipliedBy(cs('sale')->apple_actual_price))
					->minus(big_decimal($this->appleTreeProduceA)->multipliedBy(cs('sale')->apple_actual_price))
					->minus(SearchCount::searchCommanderWages($this->result))
					->minus(SearchCount::searchCommissionerrWages($this->result))
					->minus(SearchCount::searchMajordomoWages($this->result))
					->minus(SearchCount::searchTraderWages($this->result))
					->minus(SearchCount::searchTechnologyWages($this->result))
					->minus(SearchCount::searchVideoWages($this->result))
					->toScale(2, RoundingMode::HALF_UP);
			$box = new InfoBox('预估会员利润统计', 'bitcoin', 'purple', '/admin/gold-coins', $money);
			$column->append($box);
		});
	}


	


}
